<?php

declare(strict_types=1);

namespace App\Application\Admin\Controller;

use App\Annotation\Api;
use App\Annotation\View;
use App\Application\Admin\Controller\RequestParam\CouponSubmitRequestParam;
use App\Application\Admin\Middleware\AdminMiddleware;
use App\Application\Admin\Model\Room;
use App\Application\Admin\Service\RoomService;
use App\Application\Admin\Service\AdminUserService;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Application\Admin\Model\Coupon;
use App\Application\Admin\Model\Store;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Middleware(AdminMiddleware::class)]
#[Controller("admin/coupon")]
class CouponController extends AbstractController
{
    #[View]
    #[GetMapping("index")]
    public function index()
    {

    }

    #[Api]
    #[GetMapping("index/lists")]
    function lists()
    {
        $where = [];
        if ($this->getadminid() > 1) {
            $where[] = ['admin_id', '=', $this->getadminid()];
        }

        $storelist = Store::where($where)
            ->orderBy('created_at', 'DESC')
            ->pluck('store_name', 'store_id');
        $coupon_name = $this->request->input('name', '');

        if ($coupon_name != '') {
            $where[] = ['name', 'like', '%' . $coupon_name . '%'];
        }
        $lists = Coupon::where($where)
            ->orderBy('coupon_id', 'DESC')
            ->paginate()->toArray();
        foreach ($lists['data'] as $k => $v) {
            //店铺名称
            $lists['data'][$k]['store_name'] = $storelist[$v['store_id']];

        }
        return compact('lists', 'storelist');
    }

    #[Api]
    #[RequestMapping("edit", ["POST", "PUT"])]
    function submitEdit()
    {
        $request_param = new CouponSubmitRequestParam();
        $request_param->validatedThrowMessage();
        $coupon_id = $request_param->getCouponId();
        if ($this->getadminid() > 1) {
            $res = Coupon::createCoupon($coupon_id, [
                'name' => $request_param->getName()
                , 'price' => $request_param->getPrice()
                , 'money' => $request_param->getMoney()
                , 'condition' => $request_param->getCondition()
                , 'roomids' => $request_param->getRoomids()
                , 'time' => $request_param->getTime()
                , 'hours_add_time' => $request_param->getHoursAddTime()
                , 'hours_end_time' => $request_param->getHoursendtime()
                , 'user_num' => $request_param->getUserNum()
                , 'num' => $request_param->getNum()
                , 'meituan_id' => $request_param->getMeituanId()
                , 'trill_id' => $request_param->getTrillId()
                , 'type' => $request_param->getType()
                , 'receive' => $request_param->getReceive()
                , 'status' => $request_param->getStatus()
                , 'end_time' => $request_param->getEndTime()
                ,'store_id' => $request_param->getStoreId()
            ]);
        }else{
            $res = Coupon::createCoupon($coupon_id, [
                'name' => $request_param->getName()
                , 'price' => $request_param->getPrice()
                , 'money' => $request_param->getMoney()
                , 'condition' => $request_param->getCondition()
                , 'roomids' => $request_param->getRoomids()
                , 'time' => $request_param->getTime()
                , 'hours_add_time' => $request_param->getHoursAddTime()
                , 'hours_end_time' => $request_param->getHoursendtime()
                , 'user_num' => $request_param->getUserNum()
                , 'num' => $request_param->getNum()
                , 'meituan_id' => $request_param->getMeituanId()
                , 'trill_id' => $request_param->getTrillId()
                , 'type' => $request_param->getType()
                , 'receive' => $request_param->getReceive()
                , 'status' => $request_param->getStatus()
                , 'end_time' => $request_param->getEndTime()
                ,'store_id' => $request_param->getStoreId()
            ]);
        }


        return $res ? $this->returnSuccessJson(compact('res', 'coupon_id')) : $this->returnErrorJson();
    }

    #[View]
    #[GetMapping("edit")]
    function edit()
    {
        $coupon_id = (int)$this->request->input('coupon_id', 0);

        return ['title' => $coupon_id > 0 ? '编辑优惠卷' : '新增优惠卷'];
    }


    #[Api]
    #[GetMapping("edit/{coupon_id}")]
    function editInfo(int $coupon_id)
    {
        $coupon = Coupon::where('coupon_id', $coupon_id)->select('*')
            ->first() ?: [];
        $roomOptions = Room::where('store_id', $coupon->store_id)->select(['name', 'room_id'])->get()->toArray();
        if ($this->getadminid() > 1) {
            $storeOptions = Store::where('admin_id', $this->getadminid())->select('*')->get()
                ->toArray();
        } else {
            $storeOptions = Store::where('admin_id', $coupon->admin_id)->select('*')->get()
                ->toArray();
        }


        return compact('coupon', 'storeOptions', 'roomOptions');
    }


}
